package com.lmscn.lms.api.model;

import java.util.List;

import com.jfinal.plugin.activerecord.Model;

/**
 * Created by quxingyu on 2015/8/6.
 */
@SuppressWarnings("serial")
public class Exam extends Model<Exam> {
    public static final Exam dao = new Exam();
    
    private String img_path;
    
    /**
     * 通过课程ID和考试类型来获得考试
     * @param id 课程ID
     * @param type 考试类型
     * */
    public Exam getCourseExam(Long id,String type){
        return dao.findFirst("select * from t_exam where target_id = ? and target_type = ?", id, type);
    }
    
    public Exam getExam(Long examId) {
        return dao.findFirst("select * from t_exam where id = ?", examId);
    }
    
    public List<Exam> getExamList(int year, Long cityId, String studyGroup) {
    	String sql = "select * from t_exam where target_type = 'yearexam' and year = " + year;
    	if (cityId!=null) {
    		sql += " and FIND_IN_SET("+ cityId +", city_ids) > 0";
    	}
    	if (studyGroup!=null) {
    		sql += " and FIND_IN_SET('"+ studyGroup +"', study_groups) > 0";
    	}
    	
    	return dao.find(sql);
    }
    
	public List<Exam> getJoinedYearStudyPlanExamList(Long studentId, int year) {
		String sql = "select * from t_exam e where e.target_id = " + year
				+ " and e.target_type = 'yearexam' and EXISTS ( select t.* from t_testpaper_result t"
				+ " where t.target_type = 'yearexam' and t.target_id = e.id and t.student_id ="
				+ studentId + " )";
		
		return dao.find(sql);
	}
    		

    public void saveExam(CourseLearningSetting courseLearningSetting,Exam exam){
        Long courseId = courseLearningSetting.getLong("course_id");
        CourseBase courseBase = CourseBase.dao.findById(courseId);
        exam.set("title",courseBase.get("name"));
        exam.set("start_time",courseLearningSetting.get("start_time"));
        exam.set("end_time",courseLearningSetting.get("end_time"));
        exam.set("pass_proportion",SysProperties.dao.getSysPropertiesByName("course_exam_pass_score"));
        exam.set("limited_time",SysProperties.dao.getSysPropertiesByName("course_exam_limited_time"));
        exam.set("score",SysProperties.dao.getSysPropertiesByName("course_exam_total_score"));
        exam.set("item_count",SysProperties.dao.getSysPropertiesByName("course_exam_total_count"));
        exam.set("times",SysProperties.dao.getSysPropertiesByName("course_exam_times"));
        exam.set("is_open","1");
        exam.set("missscore","0");
        QuestionCategory questionCategory = QuestionCategory.dao.getQuestionCategory(courseBase.getLong("id"),"course");
        if(questionCategory==null){
            exam.set("rand_categery_id","1,2");
        }else{
            exam.set("rand_categery_id",String.valueOf(questionCategory.getLong("id")));
        }
        exam.set("target_id",courseBase.get("id"));
        exam.set("target_type","courseexam");
        exam.set("paper_make_pattern","3");
        exam.set("paper_answer_pattern","2");
        exam.set("rand_single_num",SysProperties.dao.getSysPropertiesByName("course_exam_single_choice_num"));
        exam.set("rand_multi_num",SysProperties.dao.getSysPropertiesByName("course_exam_multi_choice_num"));
        exam.set("rand_choose_num",SysProperties.dao.getSysPropertiesByName("course_exam_determine_choose_num"));
        exam.set("rand_fill_num",SysProperties.dao.getSysPropertiesByName("course_exam_fill_blank_num"));
        exam.set("rand_single_score",SysProperties.dao.getSysPropertiesByName("course_exam_single_choice_score"));
        exam.set("rand_multi_score",SysProperties.dao.getSysPropertiesByName("course_exam_multi_choice_score"));
        exam.set("rand_choose_score",SysProperties.dao.getSysPropertiesByName("course_exam_determine_choose_score"));
        exam.set("rand_fill_score",SysProperties.dao.getSysPropertiesByName("course_exam_fill_blank_score"));
        exam.remove("id").save();
        courseLearningSetting.set("exam_id", exam.getLong("id"));
        courseLearningSetting.update();
    }

    /**
     * 保存班级考试
     * 试卷信息
     * @param clazzLearningSetting
     * @param exam
     */
    public void saveClassExam(ClazzLearningSetting clazzLearningSetting,Exam exam){
        Clazz clazz = Clazz.dao.findById(clazzLearningSetting.getLong("class_id"));
        exam.set("title",clazz.get("name"));
        exam.set("pass_proportion",SysProperties.dao.getSysPropertiesByName("clazz_exam_pass_score"));
        exam.set("limited_time",SysProperties.dao.getSysPropertiesByName("clazz_exam_limited_time"));
        exam.set("score",SysProperties.dao.getSysPropertiesByName("clazz_exam_total_score"));
        exam.set("item_count",SysProperties.dao.getSysPropertiesByName("clazz_exam_item_count"));
        exam.set("times",SysProperties.dao.getSysPropertiesByName("clazz_exam_times"));
        exam.set("is_open","1");
        exam.set("missscore","0");
        exam.set("target_id",clazz.get("id"));
        exam.set("target_type","faceclazzexam");
        exam.set("paper_make_pattern","3");
        exam.set("paper_answer_pattern","2");
        QuestionCategory questionCategory = QuestionCategory.dao.getQuestionCategory(clazz.getLong("id"),"clazz");
        if(questionCategory==null){
            exam.set("rand_categery_id","1,2");
        }else{
            exam.set("rand_categery_id",String.valueOf(questionCategory.getLong("id")));
        }
        exam.set("rand_single_num",SysProperties.dao.getSysPropertiesByName("clazz_exam_single_choice_num"));
        exam.set("rand_multi_num",SysProperties.dao.getSysPropertiesByName("clazz_exam_multi_choice_num"));
        exam.set("rand_choose_num",SysProperties.dao.getSysPropertiesByName("clazz_exam_determine_choose_num"));
        exam.set("rand_fill_num",SysProperties.dao.getSysPropertiesByName("clazz_exam_fill_blank_num"));
        exam.set("rand_single_score",SysProperties.dao.getSysPropertiesByName("clazz_exam_single_choice_score"));
        exam.set("rand_multi_score",SysProperties.dao.getSysPropertiesByName("clazz_exam_multi_choice_score"));
        exam.set("rand_choose_score",SysProperties.dao.getSysPropertiesByName("clazz_exam_determine_choose_score"));
        exam.set("rand_fill_score",SysProperties.dao.getSysPropertiesByName("clazz_exam_fill_blank_score"));
        exam.remove("id").save();
        clazzLearningSetting.set("exam_id",exam.get("id"));
        clazzLearningSetting.update();
    }


    /**
     * 保存我的考试
     * 试卷信息
     * @param myExamLearningSetting
     * @param exam
     */
    public void saveMyExam(MyExamLearningSetting myExamLearningSetting,Exam exam){
        MyExam myExam = MyExam.dao.findById(myExamLearningSetting.getLong("myexam_id"));
        exam.set("title",myExam.get("name"));
        exam.set("pass_proportion",SysProperties.dao.getSysPropertiesByName("myexam_exam_pass_score"));
        exam.set("limited_time",SysProperties.dao.getSysPropertiesByName("myexam_exam_limited_time"));
        exam.set("score",SysProperties.dao.getSysPropertiesByName("myexam_exam_total_score"));
        exam.set("item_count",SysProperties.dao.getSysPropertiesByName("myexam_exam_item_count"));
        exam.set("is_open","1");
        exam.set("times",SysProperties.dao.getSysPropertiesByName("myexam_exam_times"));
        exam.set("missscore","0");
        exam.set("target_id",myExam.getLong("id"));
        exam.set("target_type","myexam");
        exam.set("paper_make_pattern","3");
        exam.set("paper_answer_pattern","2");
        QuestionCategory questionCategory = QuestionCategory.dao.getQuestionCategory(myExam.getLong("id"),"myexam");
        if(questionCategory==null){
            exam.set("rand_categery_id","1,2");
        }else{
            exam.set("rand_categery_id",String.valueOf(questionCategory.getLong("id")));
        }
        exam.set("rand_single_num",SysProperties.dao.getSysPropertiesByName("myexam_exam_single_choice_num"));
        exam.set("rand_multi_num",SysProperties.dao.getSysPropertiesByName("myexam_exam_multi_choice_num"));
        exam.set("rand_choose_num",SysProperties.dao.getSysPropertiesByName("myexam_exam_determine_choose_num"));
        exam.set("rand_fill_num",SysProperties.dao.getSysPropertiesByName("myexam_exam_fill_blank_num"));
        exam.set("rand_single_score",SysProperties.dao.getSysPropertiesByName("myexam_exam_single_choice_score"));
        exam.set("rand_multi_score",SysProperties.dao.getSysPropertiesByName("myexam_exam_multi_choice_score"));
        exam.set("rand_choose_score",SysProperties.dao.getSysPropertiesByName("myexam_exam_determine_choose_score"));
        exam.set("rand_fill_score",SysProperties.dao.getSysPropertiesByName("myexam_exam_fill_blank_score"));
        exam.remove("id").save();
        myExamLearningSetting.set("exam_id",exam.get("id"));
        myExamLearningSetting.update();
    }

	public String getImg_path() {
		return img_path;
	}

	public void setImg_path(String img_path) {
		this.img_path = img_path;
	}

}
